<!DOCTYPE html>
<html>

<head>
<title>jQuery-CSV Testing</title>
<meta charset="UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="../src/jquery.csv.js"></script>
<script src="qunit.js"></script>
<link media="screen" href="qunit.css" rel="stylesheet"/>
<script>
$(document).ready(function() {

  // initialize the data for the 'core' module
  function coreDataSetUp() {
    testCSVEntry = '1,"Evans & Sutherland","230-132-111AA",,"Visual","PCB",,1,"Offsite",';

    testCSV = '';
    testCSV += 'ID,iManufacturer,iMPartNumber,iSerialNumber,iSimCategory,iPartType,iDescription,iGroup,iLocation,iSold\r\n';
    testCSV += '1,"Evans & Sutherland","230-132-111AA",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '2,"Evans & Sutherland","230-132-111AA",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '3,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '4,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '5,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '6,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '7,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '8,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '9,"Evans & Sutherland","230-120-112AC",,"Visual","PCB",,1,"Offsite",\r\n';
    testCSV += '10,"Evans & Sutherland","230-121-150AC",,"Visual","PCB",,1,"Offsite",';

    testEntryArray = ["1", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB", "", "1", "Offsite", ""];

    testArray = [];
    testArray.push(["ID", "iManufacturer", "iMPartNumber", "iSerialNumber", "iSimCategory", "iPartType", "iDescription", "iGroup", "iLocation", "iSold"]);
    testArray.push(["1", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["2", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["3", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["4", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["5", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["6", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["7", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["8", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["9", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);
    testArray.push(["10", "Evans & Sutherland", "230-121-150AC", "", "Visual", "PCB", "", "1", "Offsite", ""]);

    testDictionary = [];
    testDictionary.push({ ID:"1",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-132-111AA", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"2",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-132-111AA", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"3",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"4",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"5",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"6",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"7",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"8",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"9",   iManufacturer:"Evans & Sutherland", iMPartNumber:"230-120-112AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });
    testDictionary.push({ ID:"10",  iManufacturer:"Evans & Sutherland", iMPartNumber:"230-121-150AC", iSerialNumber:"", iSimCategory:"Visual",  iPartType:"PCB",  iDescription:"",  iGroup:"1", iLocation:"Offsite",  iSold:"" });

    testEscaping = [];
    testEscaping.push('1,"he said ""you are crazy""", "one "", two "", three """');
    testEscaping.push('2,\'he said "\'you are crazy"\'\', \'one "\', two "\', three "\'\'');
    testEscaping.push('3;"he said ""you are crazy"""; "one "", two "", three """');
    testEscaping.push('4,"he said \\"you are crazy\\"", "one \\", two \\", three \\""');
    testEscaping.push('5|"he said ""you are crazy"""| "one "", two "", three """');
    resultEscaping = [];
    resultEscaping.push(['1', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['2', 'he said \'you are crazy\'', 'one \', two \', three \'']);
    resultEscaping.push(['3', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['4', 'he said "you are crazy"', 'one ", two ", three "']);
    resultEscaping.push(['5', 'he said "you are crazy"', 'one ", two ", three "']);

    testNewLines = '';
    testNewLines += '"-1", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"\r\n';
    testNewLines += '"0", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"\r\n';
    testNewLines += '"1", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"\r\n';
    testNewLines += '"2", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"\r\n';
    testNewLines += '"3", "Evans & Sutherland", "230-120-112AC", "", "Visual", "PCB"\r\n';
    resultNewLines = [];
    resultNewLines.push(["-1", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["0", "Evans & Sutherland", "230-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["1", "Evans & Sutherland", "230\r\n-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["2", "Evans & Sutherland", "230\r-132-111AA", "", "Visual", "PCB"]);
    resultNewLines.push(["3", "Evans & Sutherland", "230\n-120-112AC", "", "Visual", "PCB"]);
  }

  module("core", { setup:function(){ coreDataSetUp() }});
  test("CSV Entry Parsing", function() {
    expect(1);
    stop();
    var output = $.csvEntry2Array(testCSVEntry);
    start();
    deepEqual(output, testEntryArray, "csvEntry2Array()");
    //stop();
    //var output = $.array2CSVEntry(testEntryArray);
    //start();
    //equal(output, testCSVEntry, "array2CSVEntry()");
  });

  test("CSV Array Parsing", function() {
    expect(1);
    stop();
    var output = $.csv2Array(testCSV);
    start();
    deepEqual(output, testArray, "csv2Array()");
    //stop()
    //var output = $.array2CSV(testArray);
    //start()
    // TODO: Add test
  });

  test("Dictionary Parsing", function() {
    expect(1);
    stop();
    var output = $.csv2Dictionary(testCSV);
    start();
    deepEqual(output, testDictionary, "csv2Dictionary()");
    //stop();
    //var output = $.dictionary2CSV(testDictionary);
    //start();
    // TODO: Add test
  });

  test("Custom Delimiter, Separator, and Escaper Characters", function() {
    expect(5);
    stop();
    var output = $.csvEntry2Array(testEscaping[0]);
    start();
    deepEqual(output, resultEscaping[0], "standard delimiter/separator/escaper");
    stop();
    var output = $.csvEntry2Array(testEscaping[1], {
      delimiter:"'"
    });
    start();
    deepEqual(output, resultEscaping[1], "custom delimiter");
    stop();
    var output = $.csvEntry2Array(testEscaping[2], {
      separator:';'
    });
    start();
    deepEqual(output, resultEscaping[2], "custom separator");
    stop();
    var output = $.csvEntry2Array(testEscaping[3], {
      escaper:'\\'
    });
    start();
    deepEqual(output, resultEscaping[3], "custom escaper");
    stop();
    var output = $.csvEntry2Array(testEscaping[4], {
      separator:'|'
    });
    start();
    deepEqual(output, resultEscaping[4], "regex special char");
  });

//  test("Edge Cases", function() {
//    expect(1);
//    stop();
//    var output = $.csv2Array(testNewLines);
//    start();
//    deepEqual(output, resultNewLines, "parsing fields that contain newline chars");
//  });
});
</script>
</head>

<body>
  <h1 id="qunit-header">jQuery-CSV Testing</h1>
  <h2 id="qunit-banner"></h2>
  <h2 id="qunit-userAgent"></h2>
  <ol id="qunit-tests">
  </ol>
</body>

</html>
